package com.cys.markdown.doc.admin.controller;


import com.cys.common.data.RespCodeEnum;
import com.cys.common.pojo.RespEntity;
import com.cys.common.util.base.StringUtil;
import com.cys.common.util.collection.CollectionUtil;
import com.cys.markdown.doc.admin.common.annotation.CurrentUser;
import com.cys.mid.core.system.interfaces.entity.ISysRight;
import com.cys.mid.core.system.interfaces.entity.ISysUser;
import com.cys.mid.core.system.interfaces.service.SysRightService;
import com.cys.mid.core.system.interfaces.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/main")
@Transactional
public class MainController {

    private Logger logger = LoggerFactory.getLogger(MainController.class);

    @Resource(name = "sysRightServiceImpl")
    private SysRightService sysRightService;

    @Resource(name = "sysUserServiceImpl")
    private SysUserService sysUserService;

    /**
     * 检查session是否存在
     */
    @RequestMapping(value = "/checkSession", method = RequestMethod.GET)
    @ResponseBody
    public RespEntity checkSession(@CurrentUser Long sysUserId) {
        if (!StringUtil.checkObj(sysUserId)) {
            return new RespEntity(RespCodeEnum.session_timeout);
        }
        return new RespEntity(RespCodeEnum.success);
    }

    /**
     * 根据用户获取权限菜单
     *
     * @param sysUserId 当前登陆用户id
     */
    @RequestMapping(value = "/getMenu", method = RequestMethod.GET)
    @ResponseBody
    public RespEntity getMenu(@CurrentUser Long sysUserId) {
        logger.debug("method[getMenus] sysUserId : " + sysUserId);
        if (!StringUtil.checkObj(sysUserId)) {
            return new RespEntity(RespCodeEnum.session_timeout);
        }
        List<ISysRight> treeList = sysRightService.findMenuListByUserId(sysUserId);
        List<Map> resultList = new ArrayList<>();
        for (ISysRight sysRight : treeList) {
            Map result = convertMenuMap(sysRight);
            resultList.add(result);
        }
        return new RespEntity(RespCodeEnum.success, resultList);
    }

    /**
     * 获取个人信息
     *
     * @param sysUserId 当前登陆用户id
     */
    @GetMapping("/getUserInfo")
    @ResponseBody
    public RespEntity getUserInfo(@CurrentUser Long sysUserId) {
        logger.debug("method[getUserInfo] sysUserId : " + sysUserId);
        if (!StringUtil.checkObj(sysUserId)) {
            return new RespEntity(RespCodeEnum.session_timeout);
        }
        ISysUser sysUser = sysUserService.find(sysUserId);
        return new RespEntity(RespCodeEnum.success, sysUser);
    }


    /**
     * 根据权限属性转换成前端的调用的数据格式
     *
     * @param sysRight 权限
     * @return
     */
    private static Map convertMenuMap(ISysRight sysRight) {
        Map result = new HashMap();
        if (!CollectionUtil.isEmpty(sysRight.getChildrenList())) {
            //有子权限
            List<Map> childrenList = new ArrayList<>();
            for (ISysRight childrenSysRight : sysRight.getChildrenList()) {
                Map childrenMap = convertMenuMap(childrenSysRight);
                childrenList.add(childrenMap);
            }
            result.put("children", childrenList);
            result.put("isContent", false);
        }
        result.put("name", sysRight.getSysRightName());
        result.put("index", sysRight.getSysRightUrl());
        result.put("icon", sysRight.getIcon());
        return result;
    }


}
